package org.mogoo.simple;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * Created with IntelliJ IDEA.
 * Author: Fang
 * Date: 2012-08-20
 * Time: 17:40
 * Email: dongfang@mogoo.org
 */
public class IOSample
{
    public static void main(String[] args) throws Exception
    {
        File file = new File("text.txt");
        if(!file.exists())
        {
            file.createNewFile();

        }
        //使用字节流从文件读取出数据
        FileInputStream fis = new FileInputStream("file_name");
        byte[] buffer = new byte[1024];
        int hasRead= 0;
        while((hasRead = fis.read(buffer)) > 0 )
        {
            System.out.print(new String(buffer, 0, hasRead ));
        }
        //使用字节流从文件读取数据
        FileReader fr = new FileReader("file_name");
        char[] buff = new char[32];
        int hasRead2 = 0;
        while((hasRead = fr.read(buff)) >0)
        {
            System.out.print(new String(buff,0,hasRead2));
        }
        //使用字节流复制文件
        FileInputStream fiss = new FileInputStream("File_Name");
        FileOutputStream foss = new FileOutputStream("New_File_Name");

        byte[] buff3 = new byte[1024];
        int hasRead3 = 0;
        while((hasRead3 = fiss.read(buff3)) > 0)
        {
            foss.write(buff3);
        }
        foss.flush();

        {
            //使用channel和byteBuffer来读取写入
            File f = new File("file name");
            FileChannel inChannel = new FileInputStream(f).getChannel();
            MappedByteBuffer buf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, f.length());

            FileChannel outChannel = new FileOutputStream("file_name").getChannel();
            outChannel.write(buf);

            Charset charset = Charset.forName("GBK");
            buf.clear();
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer cb = decoder.decode(buf);

            Class vv = IOSample.class;









        }





    }
}
